﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.ObjectModel;


namespace SilverlightHVP.ViewModel
{
   public class ItemsViewModel : INotifyPropertyChanged
   {
      private State state;
      public ObservableCollection<Item> Items
      {
         get { return state.Items; }
      }


      public Item CurrentItem
      {
         get { return state.CurrentItem; }
         set 
         {
            if ( value != null )
            {
               state.CurrentItem = value;
               NotifyPropertyChanged( "CurrentItem" );
            }
         }
      }

      public ItemsViewModel( State state )
      {
         this.state = state;
         this.state.StateChanged += new State.StateChangedHandler( StateChanged );
         UpdateView();
      }

      private void StateChanged( object sender, EventArgs e )
      {
         UpdateView();
      }

      private void UpdateView()
      {
         NotifyPropertyChanged( "Items" );
         NotifyPropertyChanged( "CurrentItem" );
      }



      public event PropertyChangedEventHandler PropertyChanged;
      private void NotifyPropertyChanged(string propName)
      {
         if (PropertyChanged != null )
         {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
         }
      }
   }
}
